package com.huazai.apply.manager.web.filter;

import com.huazai.apply.manager.bean.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author EZTTU
 */
public class LoginCheckFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        HttpSession session = request.getSession();
        User userMessage = (User) session.getAttribute("UserMessage");
        String servletPath = request.getServletPath();

        if ("/register.jsp".equals(servletPath) || "/login.jsp".equals(servletPath) || userMessage != null) {
            filterChain.doFilter(request, response);
        } else {
            response.sendRedirect(request.getContextPath());
        }
    }
}
